package com.babasport.web.action.product;

import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.springframework.stereotype.Controller;

import com.babasport.bean.PageView;
import com.babasport.bean.QueryResult;
import com.babasport.bean.product.ProductType;
import com.babasport.service.product.ProductTypeService;
import com.babasport.web.formbean.product.ProductTypeForm;


@Controller("/control/product/type/manage")
public class ProductTypeAction extends DispatchAction {

	private static final Logger logger = Logger
			.getLogger(ProductTypeAction.class);

	@Resource(name = "productTypeServiceImpl")
	private ProductTypeService productTypeService;

	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm productTypeForm = (ProductTypeForm) form;
		PageView<ProductType> pageView = new PageView<ProductType>(12,
				productTypeForm.getPage());
		int firstIndex = (pageView.getCurrentpage() - 1)
				* pageView.getMaxresult();
		
		HashMap<String, Object> where = new HashMap<String, Object>();
		where.put("visible=", true);
		if (productTypeForm.getParentid() != null
				&& productTypeForm.getParentid() > 0) {
			where.put("parent=", productTypeForm.getParentid());
		} else {
			where.put("parent=", null);
		}
		
		if (productTypeForm.getName() != null
				&& !"".equals(productTypeForm.getName().trim())) {
			where.put("name like '%", productTypeForm.getName().trim() + "%'");
		}
		
		LinkedHashMap<String, String> orderBy = new LinkedHashMap<String, String>();
		orderBy.put("typeId", "desc");
		
		QueryResult<ProductType> qr = productTypeService.getScrollData(
				ProductType.class, firstIndex, pageView.getMaxresult(), where,
				orderBy);
		pageView.setQueryResult(qr);
		request.setAttribute("pageView", pageView);
		return mapping.findForward("listUI");
	}
	
	public ActionForward addUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("addUI");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm productTypeForm = (ProductTypeForm) form;
		ProductType productType = new ProductType(productTypeForm.getName(), productTypeForm.getNote());
		if(productTypeForm.getParentid() != null && productTypeForm.getParentid() > 0){
			productType.setParent(new ProductType(productTypeForm.getParentid()));
		}
		productTypeService.save(productType);
		request.setAttribute("message", "添加类型成功!");
		request.setAttribute("urladdress", "/control/product/type/manage.do?method=list");
		return mapping.findForward("message");
	}
	
	public ActionForward editUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm productTypeForm = (ProductTypeForm) form;
		ProductType productType = productTypeService.query(ProductType.class, productTypeForm.getTypeid());
		productTypeForm.setName(productType.getName());
		productTypeForm.setNote(productType.getNote());
		return mapping.findForward("editUI");
	}
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ProductTypeForm productTypeForm = (ProductTypeForm) form;
		ProductType productType = productTypeService.query(ProductType.class, productTypeForm.getTypeid());
		productType.setName(productTypeForm.getName());
		productType.setNote(productTypeForm.getNote());
		productTypeService.update(productType);
		request.setAttribute("message", "修改类型成功!");
		request.setAttribute("urladdress", "/control/product/type/manage.do?method=list");
		return mapping.findForward("message");
	}
	
	public ActionForward queryUI(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return mapping.findForward("queryUI");
	}
}
